package com.fulihui.wgj.consumer.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.constants.TopicConstants;
import com.fulihui.common.enums.PreAuthTypeEnum;
import com.fulihui.wgj.facade.msg.EventMsg;
import com.fulihui.wgj.facade.request.system.WxSystemConfigRequest;
import com.fulihui.wgj.facade.service.platform.WxPlatformService;
import com.fulihui.wgj.facade.service.system.WxSystemCongfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 微信预授权监听
 *
 * @author: LiuJie
 * @version: 2018/6/20 16:00
 * @description:
 */
@Component
@Slf4j
public class PreAuthListener extends  AbstractListener<EventMsg> {

    @Reference(version = "1.0.0")
    WxPlatformService wxPlatformService;
    @Reference(version = "1.0.0")
    WxSystemCongfigService wxSystemCongfigService;

    @Override
    @KafkaListener(topics = TopicConstants.PRE_AUTH)
    public void listener(ConsumerRecord<String,String> record) {
        EventMsg message = super.unpack(record.value(), EventMsg.class);

        if (PreAuthTypeEnum.authorized.getCode().equals(message.getInfoType())) {
            //授权成功
            wxPlatformService.authorized(message.getAuthorizationCode());
            log.info("权限操作={}", PreAuthTypeEnum.authorized.getDesc());
        } else if (PreAuthTypeEnum.unauthorized.getCode().equals(message.getInfoType())) {
            //取消授权
            wxPlatformService.unauthorized(message.getAuthorizerAppid());
            log.info("权限操作={}", PreAuthTypeEnum.unauthorized.getDesc());
        } else if (PreAuthTypeEnum.updateauthorized.getCode().equals(message.getInfoType())) {
            //授权更新
            wxPlatformService.updateAuthorized(message.getAuthorizationCode());
            log.info("权限操作={}", PreAuthTypeEnum.updateauthorized.getDesc());
        } else if (PreAuthTypeEnum.component_verify_ticket.getCode().equals(message.getInfoType())) {
            //更新票据
            WxSystemConfigRequest request = new WxSystemConfigRequest();
            request.setCode(PreAuthTypeEnum.component_verify_ticket.getCode());
            request.setDescript(message.getComponentVerifyTicket());
            wxSystemCongfigService.refreshConfig(request);
            log.info("权限操作={}", PreAuthTypeEnum.component_verify_ticket.getDesc());
        }
    }

}
